/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package net.sf.odinms.net.channel.handler;

import net.sf.odinms.client.IEquip;
import net.sf.odinms.client.IItem;
import net.sf.odinms.client.ItemFlag;
import net.sf.odinms.client.MapleClient;
import net.sf.odinms.client.MapleInventory;
import net.sf.odinms.client.MapleInventoryType;
import net.sf.odinms.net.AbstractMaplePacketHandler;
import net.sf.odinms.server.MapleItemInformationProvider;
import net.sf.odinms.tools.MaplePacketCreator;
import net.sf.odinms.tools.data.input.SeekableLittleEndianAccessor;

/**
 *
 * @author Small丶Hyun
 */
public final class 潜能附加 extends AbstractMaplePacketHandler {

    public final void handlePacket(SeekableLittleEndianAccessor slea, MapleClient c) {
        System.out.println("潜能附加："+slea.toString());
		slea.skip(4);
		byte slot = (byte)slea.readShort();
		byte dst = (byte)slea.readShort();
		slea.readByte();
		MapleItemInformationProvider ii = MapleItemInformationProvider.getInstance();
		IEquip toScroll;
		if (dst < 0)
			toScroll = (IEquip)c.getPlayer().getInventory(MapleInventoryType.EQUIPPED).getItem(dst);
		else
			toScroll = (IEquip)c.getPlayer().getInventory(MapleInventoryType.EQUIP).getItem(dst);
		if (toScroll == null)
			return;
		byte oldLevel = toScroll.getIdentify();
		if (oldLevel == 4 || oldLevel == 0)
		{
			MapleInventory useInventory = c.getPlayer().getInventory(MapleInventoryType.USE);
			IItem scroll = useInventory.getItem(slot);
			if (scroll.getQuantity() <= 0)
				return;
			IEquip.ScrollResult scrollSuccess = IEquip.ScrollResult.CURSE;
			switch (scroll.getItemId())
			{
			case 2049400:
			{
				int chance = (int)(Math.random() * 100D);
				if (chance < 90)
				{
					toScroll.setIdentify((byte)1);
					chance = (int)(Math.random() * 100D);
					if (chance < 30)
					{
						toScroll.setPotential_1((short)1);
						toScroll.setPotential_2((short)1);
						toScroll.setPotential_3((short)1);
					} else
					{
						toScroll.setPotential_1((short)1);
						toScroll.setPotential_2((short)1);
						toScroll.setPotential_3((short)0);
					}
					scrollSuccess = IEquip.ScrollResult.SUCCESS;
				} else
				{
					scrollSuccess = IEquip.ScrollResult.CURSE;
				}
				break;
			}

			case 2049401:
			case 2049404:
			{
				int chance = (int)(Math.random() * 100D);
				if (chance < 70)
				{
					toScroll.setIdentify((byte)1);
					chance = (int)(Math.random() * 100D);
					if (chance < 30)
					{
						toScroll.setPotential_1((short)1);
						toScroll.setPotential_2((short)1);
						toScroll.setPotential_3((short)1);
					} else
					{
						toScroll.setPotential_1((short)1);
						toScroll.setPotential_2((short)1);
						toScroll.setPotential_3((short)0);
					}
					scrollSuccess = IEquip.ScrollResult.SUCCESS;
				} else
				{
					scrollSuccess = IEquip.ScrollResult.CURSE;
				}
				break;
			}

			case 2049402:
			{
				int chance = (int)(Math.random() * 100D);
				toScroll.setIdentify((byte)1);
				chance = (int)(Math.random() * 100D);
				if (chance < 30)
				{
					toScroll.setPotential_1((short)1);
					toScroll.setPotential_2((short)1);
					toScroll.setPotential_3((short)1);
				} else
				{
					toScroll.setPotential_1((short)1);
					toScroll.setPotential_2((short)1);
					toScroll.setPotential_3((short)0);
				}
				scrollSuccess = IEquip.ScrollResult.SUCCESS;
				break;
			}

			case 2049403:
			default:
			{
				return;
			}
			}
			useInventory.removeItem(scroll.getPosition(), (short)1, false);
			byte flag = (byte) toScroll.getFlag();
			boolean bh = ItemFlag.FLAG_FB.check(flag);
			if (bh)
				toScroll.setFlag((byte)(flag - (byte)ItemFlag.FLAG_FB.getValue()));
			if (scrollSuccess == IEquip.ScrollResult.CURSE)
			{
				if (bh)
				{
					if (toScroll.getIdentify() >= 8)
					{
						c.getSession().write(MaplePacketCreator.scrolledItem(scroll, toScroll, true));
						if (dst < 0)
							c.getPlayer().getInventory(MapleInventoryType.EQUIPPED).removeItem(toScroll.getPosition());
						else
							c.getPlayer().getInventory(MapleInventoryType.EQUIP).removeItem(toScroll.getPosition());
						c.getPlayer().dropMessage("保护之盾没有生效 (8星以上装备)");
					} else
					{
						scrollSuccess = IEquip.ScrollResult.FAIL;
						c.getSession().write(MaplePacketCreator.scrolledItem(scroll, toScroll, false));
					}
				} else
				{
					c.getSession().write(MaplePacketCreator.scrolledItem(scroll, toScroll, true));
					if (dst < 0)
						c.getPlayer().getInventory(MapleInventoryType.EQUIPPED).removeItem(toScroll.getPosition());
					else
						c.getPlayer().getInventory(MapleInventoryType.EQUIP).removeItem(toScroll.getPosition());
				}
			} else
			{
				c.getSession().write(MaplePacketCreator.scrolledItem(scroll, toScroll, false));
			}
			c.getPlayer().getMap().broadcastMessage(MaplePacketCreator.潜能效果(true, c.getPlayer().getId(), scrollSuccess, false));
			c.getPlayer().equipChanged();
			c.getPlayer().saveToDB(true);
		} else
		{
			c.getPlayer().弹窗("不能使用卷轴的道具。");
		}
		c.getSession().write(MaplePacketCreator.enableActions());
	}
}